﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Compiler
{
    /// <summary>
    /// 预测分析表
    /// </summary>
    public class PredictiveAnalyzeTable
    {
        /// <summary>
        /// 预测分析表
        /// </summary>
        public static Dictionary<string, List<int>> Table = new Dictionary<string, List<int>>();

        /// <summary>
        /// 终结符 及坐标映射
        /// </summary>
        public static Dictionary<string, int> Terminator = new Dictionary<string, int>();

        /// <summary>
        /// 从文件中读取预测分析表
        /// </summary>
        public static void LoadPredictiveAnalyzeTable()
        {
            try
            {
                using (StreamReader reader = new StreamReader("../../Files/table.txt"))
                {
                    //表头及非终结符索引
                    string line = reader.ReadLine();
                    var temp = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                    //Compiler.WriteLine(line);
                    for (int i = 0; i < temp.Length; i++)
                    {
                        Terminator.Add(temp[i], i);
                    }
                    //表体及产生式
                    for (line = reader.ReadLine(); line != null; line = reader.ReadLine())
                    {
                        //Compiler.WriteLine(line);
                        var a = line.Split(new char[] { }, StringSplitOptions.RemoveEmptyEntries);
                        Table.Add(a[0], new List<int>());
                        for (int i = 1; i < a.Length; i++)
                        {
                            Table[a[0]].Add(Convert.ToInt32(a[i]));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Compiler.Log(e);
                return;
            }
        }
    }
}
